package com.zhangzhan.gulimallproduct.app;

import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.R;
import com.zhangzhan.gulimallproduct.entity.CategoryEntity;
import com.zhangzhan.gulimallproduct.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;




/**
 * 商品三级分类
 *
 * @author zhangzhan
 * @email sunlightcs@gmail.com
 * @date 2024-06-12 16:45:44
 */
@RestController
@RequestMapping("/gulimallproduct/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 查询所有商品分类展示树形结构
     */
    @RequestMapping(value = "/listTree",method = RequestMethod.GET)
    //@RequiresPermissions("gulimallproduct:category:list")
    public R listTree(){
        return categoryService.listTree();
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallproduct:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("gulimallproduct:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallproduct:category:save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改节点顺序
     */
    @RequestMapping("/update/sort")
    //@RequiresPermissions("gulimallproduct:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallproduct:category:update")
    public R update(@RequestBody CategoryEntity category){
		categoryService.updateByIdAndintermediateTable(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    //@RequiresPermissions("gulimallproduct:category:delete")
    public R delete(@RequestBody Long[] catIds){
//		categoryService.removeByIds(Arrays.asList(catIds));
		categoryService.removeMenuByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
